#include "qtvideotest.h"

#include <QDebug>
#include <QUrl>
#include <QMetaType>

QtVideoTest::QtVideoTest(QWidget *parent)
    : QWidget(parent)
{
    qRegisterMetaType<QVideoFrame>("QVideoFrame&");
    mediaPlayer = new QMediaPlayer;
    VideoSurface *videoSurface = new VideoSurface();
    mediaPlayer->setVideoOutput(videoSurface);
    mediaPlayer->setMedia(QUrl("211.mp4"));
    mediaPlayer->setMuted(false);
    mediaPlayer->play();
    connect(videoSurface, SIGNAL(frameAvailable(QVideoFrame &)), this, SLOT(ProcessFrame(QVideoFrame &)), Qt::QueuedConnection);
}

QtVideoTest::~QtVideoTest()
{
}

void QtVideoTest::ProcessFrame(QVideoFrame &frame)
{
    static int count=0;
    count++;
    if (count % 5 == 0)
    {
        mediaPlayer->pause();
        frame.map(QAbstractVideoBuffer::ReadOnly);
        QImage recvImage(frame.bits(), frame.width(), frame.height(), QVideoFrame::imageFormatFromPixelFormat(frame.pixelFormat()));
        recvImage.save(QString("./%1.jpg").arg(count), "JPG");
        frame.unmap();
        qDebug() << "test";
        mediaPlayer->play();
    }
}
